﻿package Engine.RMI
{
    import Framework.Holder.*;
    import Framework.MQ.*;
    import Framework.Serialize.*;
    import Framework.Util.*;

    public class Session extends Object
    {
        private var _communicator:Communicator;
        private var _connection:RMIConnection;
        private var _messageMap:IDMap;
        private var _messageWaitToSend:Array;
        private var _bindingObject:Array;
        private var _keepActivePrx:IKeepActivePrxHelper;
        private var _name:String;
        private var _messageHandler:IMessageHandler;

        public function Session()
        {
            this._messageWaitToSend = new Array();
            this._bindingObject = new Array();
            this._messageMap = new IDMap();
            this._keepActivePrx = new IKeepActivePrxHelper();
            this._keepActivePrx.bindingSession(this);
            return;
        }// end function

        public function get keepActivePrx() : IKeepActivePrx
        {
            return this._keepActivePrx;
        }// end function

        public function get connection() : RMIConnection
        {
            return this._connection;
        }// end function

        public function set connection(param1:RMIConnection) : void
        {
            this._connection = param1;
            return;
        }// end function

        public function get communicator() : Communicator
        {
            return this._communicator;
        }// end function

        public function set communicator(param1:Communicator) : void
        {
            this._communicator = param1;
            return;
        }// end function

        public function get name() : String
        {
            return this._name;
        }// end function

        public function set name(param1:String) : void
        {
            this._name = param1;
            return;
        }// end function

        public function get messageHandler() : IMessageHandler
        {
            return this._messageHandler;
        }// end function

        public function set messageHandler(param1:IMessageHandler) : void
        {
            this._messageHandler = param1;
            return;
        }// end function

        public function bindCommunicator(param1:Communicator) : void
        {
            if (this._connection != null)
            {
                this._connection.close();
                this._connection = null;
            }
            this.communicator = param1;
            this.communicator.session = this;
            return;
        }// end function

        public function removeBackObject(param1:int) : Boolean
        {
            return this._messageMap.remove(param1);
        }// end function

        public function findRemoveBackObject(param1:int) : RMIObjectBind
        {
            var _loc_2:* = this._messageMap.findAndRemove(param1) as RMIObjectBind;
            if (_loc_2 != null)
            {
                _loc_2.resertTimer();
            }
            return _loc_2;
        }// end function

        public function addBackObject(param1:RMIObjectBind, param2:int) : int
        {
            var _loc_3:* = this._messageMap.insert(param1);
            if (_loc_3 > 0)
            {
                param1.initTimer(param2);
            }
            return _loc_3;
        }// end function

        public function findObject(param1:SIdentity) : RMIObject
        {
            var _loc_2:RMIObject = null;
            var _loc_3:int = 0;
            while (_loc_3 < this._bindingObject.length)
            {
                
                _loc_2 = this._bindingObject[_loc_3] as RMIObject;
                if (_loc_2.identity.name == param1.name)
                {
                    return _loc_2;
                }
                _loc_3++;
            }
            return null;
        }// end function

        public function bindingObject(param1:SIdentity, param2:RMIObject) : Boolean
        {
            var _loc_3:RMIObject = null;
            var _loc_4:int = 0;
            while (_loc_4 < this._bindingObject.length)
            {
                
                _loc_3 = this._bindingObject[_loc_4] as RMIObject;
                if (_loc_3.identity.name == param1.name)
                {
                    return false;
                }
                _loc_4++;
            }
            param2.identity = param1;
            this._bindingObject.push(param2);
            return true;
        }// end function

        public function remove(param1:SIdentity) : Boolean
        {
            var _loc_2:RMIObject = null;
            var _loc_3:int = 0;
            while (_loc_3 < this._bindingObject.size())
            {
                
                _loc_2 = this._bindingObject[_loc_3] as RMIObject;
                if (_loc_2.identity.name == param1.name)
                {
                    this._bindingObject.splice(_loc_3, 1);
                    return true;
                }
                _loc_3++;
            }
            return false;
        }// end function

        public function pushMessage(param1:MessageBlock) : void
        {
            var _loc_2:* = new SerializeStream();
            var _loc_3:* = new Context();
            _loc_3._session = this;
            _loc_3._connection = this.connection;
            _loc_3._messageId = 0;
            _loc_3._dispachStatus = ERMIDispatchStatus.DispatchAsync;
            _loc_3._messageType = ERMIMessageType.MessageTypeMQ;
            param1.__write(_loc_2);
            Outgoing.invokeAsyncNoBack(_loc_3, null, _loc_2);
            return;
        }// end function

        public function pinkMessage(param1:ArrayHolder) : void
        {
            param1.value = this._messageWaitToSend;
            this._messageWaitToSend = new Array();
            return;
        }// end function

        public function pinkRemoveMessage(param1:ArrayHolder) : void
        {
            var _loc_4:RMIObjectBind = null;
            param1.value = new Array();
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            while (_loc_3 < this._messageWaitToSend.length)
            {
                
                _loc_4 = this.findRemoveBackObject((this._messageWaitToSend[_loc_3] as RMIObjectBind).messageId);
                if (_loc_4 != null)
                {
                    _loc_4.resertTimer();
                    var _loc_5:* = _loc_2 + 1;
                    param1.value[_loc_5] = _loc_4;
                }
                _loc_3++;
            }
            this._messageWaitToSend = new Array();
            return;
        }// end function

        public function addMessage(param1:RMIObjectBind) : void
        {
            this._messageWaitToSend.push(param1);
            return;
        }// end function

        public function abandon() : void
        {
            if (this._connection != null)
            {
                this._connection.close();
                this._connection = null;
            }
            this._messageMap = new IDMap();
            this._messageWaitToSend = new Array();
            this._bindingObject = new Array();
            return;
        }// end function

    }
}
